package day11.homework.dao.Impl;

import day11.homework.bean.InfoType;
import day11.homework.dao.InfoTypeDao;
import day11.homework.utils.JDBCUtil3;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class InfoTypeDaoImpl implements InfoTypeDao {
    @Override
    public void save(InfoType info_type) {
        String sql = "insert into info_type(id, name)values(?,?)";

        Object[] strs = {info_type.getId(),info_type.getName()};
        try {
            //只管sql，参数传进来
            JDBCUtil3.executeUpdate(sql, strs);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil3.closeConn(JDBCUtil3.getCo(), JDBCUtil3.getS(), null);
        }
    }

    @Override
    public void update(InfoType infoType) {
        String sql = "update info_type set name = ? where id = ?";
        Object[] strs = {infoType.getName(),infoType.getId()};
        try {
            JDBCUtil3.executeUpdate(sql, strs);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil3.closeConn(JDBCUtil3.getCo(), JDBCUtil3.getS(), null);
        }
    }

    @Override
    public void delete(InfoType infoType) {
        String sql = "delete from info_type where id = ?";
        String sql2 = "delete from info where info_type_id = ?";
        try {
            Object[] strs = {infoType.getId()};
            JDBCUtil3.executeUpdate(sql2, strs);
            JDBCUtil3.executeUpdate(sql, strs);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil3.closeConn(JDBCUtil3.getCo(), JDBCUtil3.getS(), null);
        }
    }

    public InfoType getInfoTypeById(int id) {
        String sql = "select * from info_type where id = ?";
        ResultSet rs = null;
        try {
            Object[] strs = {id};
            rs = JDBCUtil3.executeQuery(sql, strs);
            // 返回结果集 遍历
            while (rs.next()) {
                // 创建用户对象
                InfoType infoType = new InfoType();
                int uid = rs.getInt(1);
                String name = rs.getString("name");

                infoType.setId(uid);
                infoType.setName(name);

                // 返回用户信息
                return infoType;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil3.closeConn(JDBCUtil3.getCo(), JDBCUtil3.getS(), rs);
        }
        return null;
    }

    public List<InfoType> findInfoTypes() {
        // 定义集合
        List<InfoType> infoTypes = new ArrayList<>();
        ResultSet rs = null;
        try {
            String sql = "select * from info_type";
            rs = JDBCUtil3.executeQuery(sql, null);
            // 返回结果集 遍历
            while (rs.next()) {
                // 创建用户对象
                InfoType infoType = new InfoType();
                int uid = rs.getInt(1);
                String name = rs.getString("name");

                infoType.setId(uid);
                infoType.setName(name);
                // 每个用户放到集合里面
                infoTypes.add(infoType);
            }

            return infoTypes;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil3.closeConn(JDBCUtil3.getCo(), JDBCUtil3.getS(), rs);
        }
        return null;
    }
}
